package com.example.myrecyclerview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.example.myrecyclerview.fragment.BookFragment;
import com.example.myrecyclerview.fragment.MapViewFragment;
import com.example.myrecyclerview.fragment.NewsFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    private TabLayoutMediator tabLayoutMediator;
    private ArrayList<Fragment> fragments;
    private ArrayList<String> tabs;

    public static Context context;
    public static Activity activity;

    public static Context getContext() {
        return context;
    }

    public static Activity getActivity() {
        return activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        activity = this;
        initData();
        initView();
    }

    private void initData() {
        tabs = new ArrayList<>();
        tabs.add(getString(R.string.tab_book));
        tabs.add(getString(R.string.tab_news));
        tabs.add(getString(R.string.tab_seller));

        fragments = new ArrayList<>();
        fragments.add(new BookFragment());
        fragments.add(new NewsFragment());
        fragments.add(new MapViewFragment());
    }

    private void initView() {

        tabLayout = findViewById(R.id.tablayout);
        for (int i = 0; i < tabs.size(); i++) {
            tabLayout.addTab(tabLayout.newTab());
            tabLayout.getTabAt(i).setText(tabs.get(i));
        }
        viewPager2 = findViewById(R.id.viewpager2);
        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });

        tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, true, true, (tab, position) -> tab.setText(tabs.get(position)));
        tabLayoutMediator.attach();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        tabLayoutMediator.detach();
    }

//    private void clearCookie() {
//        CookieSyncManager.createInstance(this);  //Create a singleton CookieSyncManager within a context
//        CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance
//        cookieManager.removeAllCookie();// Removes all cookies.
//        if (Build.VERSION.SDK_INT < 21) {
//            CookieSyncManager.getInstance().sync(); // forces sync manager to sync now
//        } else {
//            cookieManager.flush();
//        }
//        if (mWebMultiTime != null) {
//            mWebMultiTime.setWebChromeClient(null);
//            mWebMultiTime.setWebViewClient(null);
//            mWebMultiTime.getSettings().setJavaScriptEnabled(false);
//            mWebMultiTime.clearCache(true);
//        }
//    }
}